package com.ly.emp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.emp.constant.EmpErrorConstants;
import com.ly.emp.entity.HubTeamResultAdd;
import com.ly.emp.mapper.HubTeamResultAddMapper;
import com.ly.emp.service.IEmpCommonService;
import com.ly.emp.service.IHubTeamResultAddService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 考核结果-加减分-impl
 *
 * @author luoyun
 * @Date: 2025-05-20
 */
@Service
@RequiredArgsConstructor
public class HubTeamResultAddServiceImpl extends ServiceImpl<HubTeamResultAddMapper, HubTeamResultAdd> implements IHubTeamResultAddService {

    private final IEmpCommonService teamCommonService;

    @Override
    public R getId(String id) {
        HubTeamResultAdd entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(HubTeamResultAdd entity) {
        String orgCode = entity.getOrgCode();
        if(StrUtil.isBlank(orgCode)){
            return R.fail(EmpErrorConstants.NO_TEAM);
        }
        List<String> isIntensificationList = teamCommonService.getIsIntensificationListByOrgCode(orgCode);
        List<HubTeamResultAdd> list = this.list(
                new LambdaQueryWrapper<HubTeamResultAdd>()
                        .in(HubTeamResultAdd::getOrgCode,isIntensificationList)
                        .eq(StrUtil.isNotBlank(entity.getPeriodCode()), HubTeamResultAdd::getPeriodCode,entity.getPeriodCode())
                        .eq(StrUtil.isNotBlank(entity.getSuitUnit()),HubTeamResultAdd::getSuitUnit,entity.getSuitUnit())
                        .likeRight(StrUtil.isNotBlank(entity.getFinalScoreStatus()),HubTeamResultAdd::getFinalScore, entity.getFinalScoreStatus())
                        .orderByDesc(HubTeamResultAdd::getSysCreateDate)
        );
        return R.ok(list);
    }

    @Override
    public R getPage(HubTeamResultAdd entity, Page<HubTeamResultAdd> page) {
        String orgCode = entity.getOrgCode();
        if(StrUtil.isBlank(orgCode)){
            return R.fail(EmpErrorConstants.NO_TEAM);
        }
        List<String> isIntensificationList = teamCommonService.getIsIntensificationListByOrgCode(orgCode);
        IPage<HubTeamResultAdd> pageList = this.page(page,
                new LambdaQueryWrapper<HubTeamResultAdd>()
                        .in(HubTeamResultAdd::getOrgCode,isIntensificationList)
                        .eq(StrUtil.isNotBlank(entity.getPeriodCode()), HubTeamResultAdd::getPeriodCode,entity.getPeriodCode())
                        .like(StrUtil.isNotBlank(entity.getPsnName()), HubTeamResultAdd::getPsnName,entity.getPsnName())
                        .orderByDesc(HubTeamResultAdd::getSysCreateDate)
        );
        return R.ok(pageList);
    }

    @Override
    public R add(HubTeamResultAdd entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(HubTeamResultAdd entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<HubTeamResultAdd> dto) {
        List<HubTeamResultAdd> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }
}
